package main

import (
	"context"
	"github.com/wonderivan/logger"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
	"ucenter/app"
	"ucenter/app/router"
	_ "ucenter/boostrap"
)

func main() {
	r := router.GetRouter()
	// 通过配置文件配置
	//err := logger.SetLogger("log.json")
	//if err != nil {
	//	fmt.Println(err)
	//}
	// 指定地址和端口号
	httpServer := http.Server{
		Addr:    app.Conf.Listen,
		Handler: r,
	}
	go func() {
	Retry:
		err := httpServer.ListenAndServe()
		if err != nil {
			logger.Fatal("bind address fail, err: %s", err.Error())
			time.Sleep(time.Second * 1)
			goto Retry
		}
	}()

	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	logger.Info("shutting")
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	httpServer.Shutdown(ctx)
	logger.Info("Bye!")
}
